from django.contrib import admin
from goods.models import GoodsType, GoodsSKU, Goods, GoodsImage, IndexGoodsBanner
from goods.models import IndexPromotionBanner, IndexTypeGoodsBanner
from django.core.cache import cache
# Register your models here.
class BaseModelAdmin(admin.ModelAdmin):
    '''基本模型管理类'''
    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        from celery_tasks.tasks import generate_static_index_html
        generate_static_index_html.delay()
        # 清除缓存
        cache.delete("index_page_data")

    def delete_model(self, request, obj):
        super().delete_model(request, obj)
        from celery_tasks.tasks import generate_static_index_html
        generate_static_index_html.delay()
        # 清除缓存
        cache.delete("index_page_data")

admin.site.register(GoodsSKU)
admin.site.register(Goods)
admin.site.register(GoodsImage)

class GoodsTypeAdmin(BaseModelAdmin):
    list_display = ("name", )

admin.site.register(GoodsType, BaseModelAdmin)

class IndexGoodsBannerAdmin(BaseModelAdmin):
    pass

admin.site.register(IndexGoodsBanner, IndexGoodsBannerAdmin)

class IndexPromotionBannerAdmin(BaseModelAdmin):
    list_display = ("name", "url", )


admin.site.register(IndexPromotionBanner, IndexPromotionBannerAdmin)

class IndexTypeGoodsBannerAdmin(BaseModelAdmin):
    pass

admin.site.register(IndexTypeGoodsBanner, IndexTypeGoodsBannerAdmin)